package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.mapper.BlogInfoMapper;
import com.blog.pojo.dataobject.BlogInfo;
import com.blog.pojo.response.BlogInfoResponse;
import com.blog.service.BlogService;
import com.blog.common.utils.ConversionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service("blogServiceImpl")
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,0);
        List<BlogInfo>blogInfos=blogInfoMapper.selectList(queryWrapper);
        List<BlogInfoResponse>blogInfoResponses=blogInfos.stream()
                .map(blogInfo -> ConversionUtils.personToBlogInfoResponse(blogInfo))
                .collect(Collectors.toList());
        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {

        BlogInfo blogInfo =getBlogInfo(blogId);
        return ConversionUtils.personToBlogInfoResponse(blogInfo);
    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId){
        QueryWrapper<BlogInfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getId,blogId)
                .eq(BlogInfo::getDeleteFlag,0);
        return   blogInfoMapper.selectOne(queryWrapper);
    }



}
